<?php

require_once 'global.php';
require_once 'myPDO.php';

class Mp3
{
    private $_id = 0;
    private $_titol = 0;
    private $_artista = 0;
    private $_album = 0;
    private $_track = 0;
    private $_year = 0;
    private $_file = 0;
    private $_caratula = 0;
    private $_public = 0;
    
    
    

    public static function findbytitol($titol) {
        $dsn = DSN_BBDD;
        $user = USER_BBDD;
        $password = PASS_BBDD;

        try {
            $dbh = new mypdo($dsn, $user, $password);
        } catch (pdoexception $e) {
            echo 'connection failed: ' . $e->getmessage();
        }

        $sql = 'SELECT id FROM mp3 WHERE titol= :variable AND public=1';
        
        $sth = $dbh->prepare($sql);
        $bindparam = $sth->bindparam(':variable', $titol);

        $sth->execute();
        $result = $sth->fetchAll(PDO::FETCH_ASSOC);

        $sth->closecursor();
        $dbh = null;

        $cancoresult = null;
        foreach ($result as $array) {
            foreach ($array as $id) {
                $cancoresult[] = new Mp3($id);
            }
        }
        return $cancoresult;
    }

    public static function findbytitolplus($titol, $idusuari) {
        $dsn = DSN_BBDD;
        $user = USER_BBDD;
        $password = PASS_BBDD;
        
        
        $titol = '%'.$titol.'%';
        $cancoresult = null;

        
        // Busco canciones publicas //
        try {
            $dbh = new mypdo($dsn, $user, $password);
        } catch (pdoexception $e) {
            echo 'connection failed: ' . $e->getmessage();
        }

        $sql = "SELECT id FROM mp3 WHERE (titol LIKE :variable AND public=1)";
        
        $sth = $dbh->prepare($sql);
        $bindparam = $sth->bindparam(':variable', $titol);

        $sth->execute();
        $result = $sth->fetchAll(PDO::FETCH_ASSOC);

        $sth->closecursor();
        $dbh = null;

        foreach ($result as $array) {
            foreach ($array as $id) $cancoresult[] = $id;
        }
        
               
        // Busco canciones privadas del usuario //
        try {
            $dbh = new mypdo($dsn, $user, $password);
        } catch (pdoexception $e) {
            echo 'connection failed: ' . $e->getmessage();
        }

        $sql = "SELECT r.mp3 FROM usuarismp3 r, mp3 m WHERE r.mp3=m.id AND (m.titol LIKE :1) AND r.public=0 AND r.usuari= :2";
        
        $sth = $dbh->prepare($sql);
        $bindparam = $sth->bindparam(':1', $titol);
        $bindparam = $sth->bindparam(':2', $idusuari);

        $sth->execute();
        $result = $sth->fetchAll(PDO::FETCH_ASSOC);

        $sth->closecursor();
        $dbh = null;

        foreach ($result as $array) {
            foreach ($array as $id) $cancoresult[] =$id;

        }
        
        // Elimino los repetidos //
        $cancoresult = array_unique($cancoresult);
        
        // Creo los objetos en la misma array //
        foreach ($cancoresult as $clave => $valor) {
            $cancoresult[$clave] = new Mp3($valor);
        }
        
        return $cancoresult;
    }
    
    public static function refreshprivate($idmp3) {
        $dsn = DSN_BBDD;
        $user = USER_BBDD;
        $password = PASS_BBDD;

        try {
            $dbh = new mypdo($dsn, $user, $password);
        } catch (pdoexception $e) {
            echo 'connection failed: ' . $e->getmessage();
        }

        $sql = "SELECT * FROM usuarismp3 WHERE public=1 AND mp3= :variable";
        
        //if ($idmp3 > 0) $sql = $sql." mp3= :variable";
        
        $sth = $dbh->prepare($sql);
        $bindparam = $sth->bindparam(':variable', $idmp3);

        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_ASSOC);

        $sth->closecursor();
        $dbh = null;
        
        $canco = new Mp3($idmp3);
        if (!$result) $canco->my_set_private();
        else $canco->my_set_public();
        $canco->update();
   
        
        
        
        
    }
    
    // constructora
    public function __construct($mp3id = false) {
        if(!$mp3id) return;
         
        $dsn = DSN_BBDD;
        $user = USER_BBDD;
        $password = PASS_BBDD;

        try {
            $dbh = new mypdo($dsn, $user, $password);
        } catch (pdoexception $e) {
            echo 'connection failed: ' . $e->getmessage();
        }
        
        $sql = 'SELECT * FROM mp3 WHERE id= :id';
        
        $sth = $dbh->prepare($sql);
        $bindparam = $sth->bindparam(':id', $mp3id);

        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_ASSOC);

        $sth->closecursor();
        $dbh = null;
        
        if (!$result) throw new exception("identifiador de cancion: '{$mp3id}' no existe.");
        
        foreach ($result as $attr => $value) $this->{'_'.$attr} = $value;
    }

    public function insert() {

        $dsn = DSN_BBDD;
        $user = USER_BBDD;
        $password = PASS_BBDD;
                
        // Compruebo si la cancion esta en la BBDD

        try {
            $dbh = new myPDO($dsn, $user, $password);
        } catch (PDOException $e) {
            echo 'Connection failed: ' . $e->getMessage();
        }

        $sql = 'SELECT id FROM mp3 WHERE titol= :1 AND artista= :2 AND album= :3 AND track= :4 AND year= :5 AND file= :6 AND caratula= :7 AND public= :8';

        $sth = $dbh->prepare($sql);
        $bindParam = $sth->bindParam(':1', $this->_titol);
        $bindParam = $sth->bindParam(':2', $this->_artista);
        $bindParam = $sth->bindParam(':3', $this->_album);
        $bindParam = $sth->bindParam(':4', $this->_track);
        $bindParam = $sth->bindParam(':5', $this->_year);
        $bindParam = $sth->bindParam(':6', $this->_file);
        $bindParam = $sth->bindParam(':7', $this->_caratula);
        $bindParam = $sth->bindParam(':8', $this->_public);

        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_ASSOC);

        $sth->closeCursor();
        $dbh = null;   
        
        if (!$result) {
            // Si la cancion no esta la introduzco en la BBDD
            try {
                $dbh = new myPDO($dsn, $user, $password);
            } catch (PDOException $e) {
                echo 'Connection failed: ' . $e->getMessage();
            }

            $sql = 'INSERT INTO mp3 (titol, artista, album, track, year, file, caratula, public) VALUES (:1, :2, :3, :4, :5, :6, :7, :8)';

            $sth = $dbh->prepare($sql);
            $bindParam = $sth->bindParam(':1', $this->_titol);
            $bindParam = $sth->bindParam(':2', $this->_artista);
            $bindParam = $sth->bindParam(':3', $this->_album);
            $bindParam = $sth->bindParam(':4', $this->_track);
            $bindParam = $sth->bindParam(':5', $this->_year);
            $bindParam = $sth->bindParam(':6', $this->_file);
            $bindParam = $sth->bindParam(':7', $this->_caratula);
            $bindParam = $sth->bindParam(':8', $this->_public);

            $sth->execute();
            $sth->closeCursor();
            $dbh = null;
        }
     }
          
    public function update() {
        if (!$this->_id) throw new Exception("Mp3 needs id mp3 to call update.");

        $dsn = DSN_BBDD;
        $user = USER_BBDD;
        $password = PASS_BBDD;

        try {
            $dbh = new myPDO($dsn, $user, $password);
        } catch (PDOException $e) {
            echo 'Connection failed: ' . $e->getMessage();
        }

        $sql = 'UPDATE mp3 SET titol= :2, artista= :3, album= :4, track= :5, year= :6, file= :7, caratula= :8, public= :9  WHERE id= :1';

        $sth = $dbh->prepare($sql);
        $bindParam = $sth->bindParam(':1', $this->_id);
        $bindParam = $sth->bindParam(':2', $this->_titol);
        $bindParam = $sth->bindParam(':3', $this->_artista);
        $bindParam = $sth->bindParam(':4', $this->_album);
        $bindParam = $sth->bindParam(':5', $this->_track);
        $bindParam = $sth->bindParam(':6', $this->_year);
        $bindParam = $sth->bindParam(':7', $this->_file);
        $bindParam = $sth->bindParam(':8', $this->_caratula);
        $bindParam = $sth->bindParam(':9', $this->_public);

        $sth->execute();
        $sth->closeCursor();
        $dbh = null;
     }

     public function delete() {
        if (!$this->_id) throw new Exception("Mp3 object has no id Mp3");

        $dsn = DSN_BBDD;
        $user = USER_BBDD;
        $password = PASS_BBDD;

        try {
            $dbh = new myPDO($dsn, $user, $password);
        } catch (PDOException $e) {
            echo 'Connection failed: ' . $e->getMessage();
        }

        $sql = 'DELETE FROM mp3 WHERE id= :1';

        $sth = $dbh->prepare($sql);
        $bindParam = $sth->bindParam(':1', $this->_id);

        $sth->execute();
        $sth->closeCursor();
        $dbh = null;
     }
    
        
     
     public function insertplus($userid) {
        $this->insert();
        
        
        // Consegueixo el id del mp3
        $dsn = DSN_BBDD;
        $user = USER_BBDD;
        $password = PASS_BBDD;
        
        // Compruebo si la cancion esta en la BBDD

        try {
            $dbh = new myPDO($dsn, $user, $password);
        } catch (PDOException $e) {
            echo 'Connection failed: ' . $e->getMessage();
        }

        $sql = 'SELECT id FROM mp3 WHERE titol= :1 AND artista= :2 AND album= :3 AND track= :4 AND year= :5 AND file= :6 AND caratula= :7 AND public= :8';

        $sth = $dbh->prepare($sql);
        $bindParam = $sth->bindParam(':1', $this->_titol);
        $bindParam = $sth->bindParam(':2', $this->_artista);
        $bindParam = $sth->bindParam(':3', $this->_album);
        $bindParam = $sth->bindParam(':4', $this->_track);
        $bindParam = $sth->bindParam(':5', $this->_year);
        $bindParam = $sth->bindParam(':6', $this->_file);
        $bindParam = $sth->bindParam(':7', $this->_caratula);
        $bindParam = $sth->bindParam(':8', $this->_public);

        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_ASSOC);

        $sth->closeCursor();
        $dbh = null;
        
        
        $idcanco = $result['id'];
        
   
        // Afegeixo el Mp3 al usuari que la pujat
        $dsn = DSN_BBDD;
        $user = USER_BBDD;
        $password = PASS_BBDD;
        
        // Compruebo si la cancion esta en la BBDD

        try {
            $dbh = new myPDO($dsn, $user, $password);
        } catch (PDOException $e) {
            echo 'Connection failed: ' . $e->getMessage();
        }

        $sql = 'INSERT INTO usuarismp3 (usuari, mp3) VALUES (:1, :2)';

        $sth = $dbh->prepare($sql);
        $bindParam = $sth->bindParam(':1', $userid);
        $bindParam = $sth->bindParam(':2', $idcanco);

        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_ASSOC);

        $sth->closeCursor();
        $dbh = null;
        
        
        
        
        
             
         
     }
     
     
     
     
     
     
    /* privadas */


    private function comprovar_file($path) {
        //if (!preg_match('/\w/', $path)) throw new exception("error: fitxer");     
    }
     
    private function comprovar_caratula($caratula) {
        //if (!preg_match('/\w/', $path)) throw new exception("error: fitxer");     
    }
    
    private function comprovar_artista($artista) {
        if (!preg_match('/\w/', $artista)) throw new exception("error: artista"); 
        
    }
    
    private function comprovar_titol($titol) {
        if (!preg_match('/\w/', $titol)) throw new exception("error: nom titol"); 
        
    }
    
    private function comprovar_track($track) {
        if (!preg_match('/[0-9]{1,3}/', $track)) throw new exception("error: nom track");
        
    }
    
    private function comprovar_year($any) {
        if (!preg_match('/[0-9]{4}/', $any)) throw new exception("error: any");
        
        
    }
    
    private function comprovar_album($album) {
        if (!preg_match('/\w/', $album)) throw new exception("error: nom album");
        
    }
    
    private function my_set_public() {
        $this->_public = 1;
    }
    
    private function my_set_private() {
        $this->_public = 0;
    }


    /* publicas */
    
      
    // gets
    public function get_id() {
        return $this->_id;
    }
    
    public function get_file() {
        return $this->_file;
    }
    
    public function get_artista() {
        return $this->_artista;
    }
    
    public function get_titol() {
        return $this->_titol;
    }
    
    public function get_track() {
        return $this->_track;
    }

    public function get_year() {
        return $this->_year;
    }
    
    public function get_album() {
        return $this->_album;
    }
    
    public function get_caratula() {
        return $this->_caratula;
    }
    
    public function get_public() {
        return $this->_public;
    }

    public function get_publicusuari($idusuari) {
        $dsn = DSN_BBDD;
        $user = USER_BBDD;
        $password = PASS_BBDD;

        try {
            $dbh = new mypdo($dsn, $user, $password);
        } catch (pdoexception $e) {
            echo 'connection failed: ' . $e->getmessage();
        }

        $sql = 'SELECT public FROM usuarismp3 WHERE usuari= :1 AND mp3= :2';

        $sth = $dbh->prepare($sql);
        $bindparam = $sth->bindparam(':1', $idusuari);
        $bindparam = $sth->bindparam(':2', $this->_id);

        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_ASSOC);

        $sth->closecursor();
        $dbh = null;
        
        return ($result['public']);
        
    }
    //sets

    public function set_file($path) {
        $this->comprovar_file($path);
        $this->_file = $path;
    }

   
    public function set_artista($artista) {
        $this->comprovar_artista($artista);
        $this->_artista = $artista;
    }

   

    public function set_titol($titol) {
        $this->comprovar_titol($titol);
        $this->_titol = $titol;
    }

   

    public function set_track($track) {
        $this->comprovar_track($track);
        $this->_track = $track;
    }

    

    public function set_year($any) {
        $this->comprovar_year($any);
        $this->_year = $any;
    }


    public function set_album($album) {
        $this->comprovar_album($album);
        $this->_album = $album;
    }
    
    public function set_caratula($caratula) {
        $this->comprovar_caratula($caratula);
        $this->_caratula = $caratula;
    }
    
    public function set_public($iduser) {        
        $dsn = DSN_BBDD;
        $user = USER_BBDD;
        $password = PASS_BBDD;

        try {
            $dbh = new myPDO($dsn, $user, $password);
        } catch (PDOException $e) {
            echo 'Connection failed: ' . $e->getMessage();
        }
        
        $sql = 'UPDATE usuarismp3 SET public=1 WHERE usuari= :1 AND mp3= :2';
        
        $sth = $dbh->prepare($sql);
        $bindParam = $sth->bindParam(':1', $iduser);
        $bindParam = $sth->bindParam(':2', $this->_id);
        
        $sth->execute();
        $sth->closeCursor();
        $dbh = null; 
        
        Mp3::refreshprivate($this->_id);
    }
    
    public function set_private($iduser) {
        $dsn = DSN_BBDD;
        $user = USER_BBDD;
        $password = PASS_BBDD;

        try {
            $dbh = new myPDO($dsn, $user, $password);
        } catch (PDOException $e) {
            echo 'Connection failed: ' . $e->getMessage();
        }
        
        $sql = 'UPDATE usuarismp3 SET public=0 WHERE usuari= :1 AND mp3= :2';
        
        $sth = $dbh->prepare($sql);
        $bindParam = $sth->bindParam(':1', $iduser);
        $bindParam = $sth->bindParam(':2', $this->_id);
        
        $sth->execute();
        $sth->closeCursor();
        $dbh = null; 
        
        Mp3::refreshprivate($this->_id);
    }    
}

?>